//
//  ShopBagTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by xielihui on 16/3/10.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "ShopBagTableViewCell.h"
#import "ProdcutDetaiModel.h"

@interface ShopBagTableViewCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIButton *deleteMerchantBtn;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *commodityCountLabel;
@property (weak, nonatomic) IBOutlet UIButton *selectCommodityBtn;
@property (weak, nonatomic) IBOutlet UIButton *minusBtn;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIView *commodityNumBackView;
@property (weak, nonatomic) IBOutlet UIImageView *comPicView;
@property (weak, nonatomic) IBOutlet UILabel *optionLabel;


@end

@implementation ShopBagTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editCommodityAction:) name:@"DonotdisturbViewController" object:nil];
    self.commodityNumBackView.layer.borderColor = [UIColor colorWithRGB:0xe1e1e1].CGColor;
    self.commodityNumBackView.layer.borderWidth = 1.f;
    self.commodityNumBackView.layer.cornerRadius = 15;
    self.comPicView.layer.borderColor = [UIColor colorWithRGB:0xe1e1e1].CGColor;
    self.comPicView.layer.borderWidth = 1.f;
}

- (void)updateWithProductModel:(CartProductModel *)model isEditing:(BOOL)isEditing {
    self.productModel = model;
    [self.productModel addObserver:self forKeyPath:@"checked" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    self.commodityCountLabel.text = [NSString stringWithFormat:@"%ld", model.quantity];
    self.priceLabel.text = model.total;
    self.nameLabel.text = model.name;
    self.optionLabel.text = model.option;
    [self.comPicView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    [self setBtnImages];
    
    if (isEditing) {
        self.deleteMerchantBtn.hidden = NO;
        self.priceLabel.hidden = YES;
    } else {
        self.deleteMerchantBtn.hidden = YES;
        self.priceLabel.hidden = NO;
    }
//    self.deleteMerchantBtn.right = self.priceLabel.right;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)editCommodityAction:(id)sender {
    self.deleteMerchantBtn.hidden = !self.deleteMerchantBtn.hidden;
    self.priceLabel.hidden = !self.priceLabel.hidden;
    self.deleteMerchantBtn.center = self.priceLabel.center;
}

- (IBAction)addCommodityNumAction:(id)sender {
    if (self.updateProductQuantity) {
        self.updateProductQuantity(self.productModel, ++self.productModel.quantity);
    }
}

- (IBAction)minusCommodityNumAction:(id)sender {
    if (self.updateProductQuantity) {
        self.updateProductQuantity(self.productModel, --self.productModel.quantity);
    }
}

- (void)updateProductQuantityInfo:(NSInteger)quantity {
    self.productModel.quantity = quantity;
    [self setBtnImages];
    self.commodityCountLabel.text = [NSString stringWithFormat:@"%ld", (long)self.productModel.quantity];
}

- (IBAction)selectCommodityAction:(id)sender {
    self.productModel.checked = !self.productModel.checked;
    if (self.selectCommodityBlock) {
        self.selectCommodityBlock(self.productModel);
    }
}

- (IBAction)deleteCommodityAction:(id)sender {
    if (self.deleteProductAction) {
        self.deleteProductAction(self.productModel);
    }
}

- (void)setBtnImages {
    if (self.productModel.quantity > self.productModel.minimum) {
        self.minusBtn.enabled = YES;
    } else {
        self.minusBtn.enabled = NO;
    }
    if (self.productModel.checked) {
        [self.selectCommodityBtn setImage:[UIImage imageNamed:@"btn_select_selectd"] forState:UIControlStateNormal];
    } else {
        [self.selectCommodityBtn setImage:[UIImage imageNamed:@"btn_select_nor"] forState:UIControlStateNormal];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"checked"]) {
        [self setBtnImages];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (void)dealloc {
    [self.productModel removeObserver:self forKeyPath:@"checked"];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
